import json


def updateJsonValue(res, keys, values):
    keys = keys.split(",")
    values = values.split(",")
    for key, value in zip(keys, values):
        for k, v in res.items():
            if k == key:
                if value.isdigit():
                    res[k] = int(value)
                elif value == 'True':
                    res[k] = True
                elif value == 'False':
                    res[k] = False
                else:
                    res[k] = value
            else:
                if type(v) is dict:
                    updateJsonValue(v, key, value)
    res = json.loads(json.dumps(res))
    return res


def update_yapi(yapi_list):

    global method, header
    result = []
    for y in yapi_list:
        parent = {
            "project_name": y["name"],
            "children": []
        }
        if y["list"]:
            for c in y["list"]:
                if c["method"] == "GET" or c["method"] == "get":
                    method = 1
                elif c["method"] == "POST" or c["method"] == "post":
                    method = 2
                elif c["method"] == "PUT" or c["method"] == "put":
                    method = 3
                elif c["method"] == "DELETE" or c["method"] == "delete":
                    method = 4
                children_dict = {
                    "url": c["query_path"]["path"],
                    "params": c["req_params"],
                    "method": method,
                    "name": c["title"]
                }
                if c.get("req_body_other") is None:
                    req_body_other = {}
                else:
                    body_data = json.loads(c.get("req_body_other"))
                    body = json.loads(json.dumps(body_data["properties"]))
                    req_body_other = {}
                    for j, i in body.items():
                        if i["type"] == "number":
                            req_body_other[j] = 0
                        elif i["type"] == "string":
                            req_body_other[j] = ""
                        elif i["type"] == "boolean":
                            req_body_other[j] = "true or false"
                        elif i["type"] == "object":
                            objects = {}
                            if i["properties"] is not None:
                                for k, l in i["properties"].items():
                                    if l["type"] == "string":
                                        objects.update({k: ""})
                                    elif l["type"] == "number" or l["type"] == "integer":
                                        objects.update({k: 0})
                                    elif l["type"] == "boolean":
                                        objects.update({k: "true or false"})
                            req_body_other[j] = objects
                        elif i["type"] == "array":
                            array = []
                            if i["items"]["type"] != "object":
                                for o, p in i["items"].items():
                                    if p == "number":
                                        value = "number"
                                        array.append(value)
                                    elif p == "string":
                                        value = "string"
                                        array.append(value)
                            elif i["items"]["type"] == "object":
                                array_dict = {}
                                for u, r in i["items"].items():
                                    if u == "properties":
                                        for a, b in r.items():
                                            if b["type"] == "string":
                                                array_dict.update({a: ""})
                                            elif b["type"] == "number":
                                                array_dict.update({a: 0})
                                array.append(array_dict)
                            req_body_other[j] = array
                children_dict.update({"body": req_body_other})
                parent["children"].append(children_dict)
        result.append(parent)
    return result
